package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanUtils;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.investor.bean.InvestorDealTransOutBean;
import com.jrx.anytxn.investor.bean.InvestorDelqTransOutSaveBean;
import com.jrx.anytxn.investor.entity.*;
import com.jrx.anytxn.investor.service.IInvestorDealDelqService;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description: 联合贷批量-转表外处理
 * @author: xiaoyu.jing
 * @date: 2020-09-01 15:06
 **/
@Order(140)
@Service
public class InvestorTransOutHandle extends BaseBatchAccoutHandle<InvestorDelqTransOutSaveBean> {

    Logger logger = LoggerFactory.getLogger(InvestorTransOutHandle.class);

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private IInvestorDealDelqService delqService;

    /**
     * 转表外处理业务逻辑
     *
     * @return
     * @throws TxnException
     */
    @Override
    public List<InvestorDelqTransOutSaveBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        CmCustomerFirstLevel firstLevel = custBatchSaveBean.getDealCust();
        String tenantId = firstLevel.getTenantId();
        PrOrganizationTable org = organizationTableService.findByOrId(TransBizConstant.DEFAULT_ORG_ID, tenantId);
        Date today = org.getToday();
        List<AmLoanAccountInvestorInfo> dealLoanAccountList = custBatchSaveBean.getDealInvestorLoanAccountList();
        if (CollectionUtils.isEmpty(dealLoanAccountList)) {
            return null;
        }
        List<AmLoanAccountInvestorInfo> transOutLoanAcctList = dealLoanAccountList.stream()
                .filter(acct -> TransBizConstant.ACCOUNT_STATUS_2.equals(acct.getAccountStatus()))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(transOutLoanAcctList)) {
            return null;
        }

        List<InvestorDelqTransOutSaveBean> delqTransOutSaveBeanList = new ArrayList<>();
        List<AmLoanMainInvestorInfo> dealLoanMainList = custBatchSaveBean.getDealInvestorLoanMainList();
        List<AmAccountMainInvestorInfo> dealAcctList = custBatchSaveBean.getDealInvestorAcctList();
        List<AmRepaymentPlanInvestorInfo> dealPlanList = custBatchSaveBean.getDealInvestorPlanList();
        List<AmInterestAccrualInvestorInfo> dealAccuAcctList = custBatchSaveBean.getDealInvestorAccuAcctList();
        for (AmLoanAccountInvestorInfo loanAcct : transOutLoanAcctList) {
            String investorLoanId = loanAcct.getInvestorLoanId();
            //添加核销订单的判断 如果是核销订单不做处理
            if (TransBizConstant.YES_FLAG.equals(loanAcct.getChargeoffFlag())) {
                logger.info("改订单已经核销不做处理，订单号：{}", loanAcct.getInvestorLoanId());
                return null;
            }
            logger.info("DelqTransIn处理loan:{}", BeanUtils.toMap(loanAcct));

            AmLoanMainInvestorInfo loanMainInfo = dealLoanMainList.stream()
                    .filter(loanMain -> investorLoanId.equals(loanMain.getInvestorLoanId()))
                    .findFirst().get();

            InvestorDealTransOutBean dealTransOutBean = new InvestorDealTransOutBean();
            dealTransOutBean.setBusinessDate(today);
            dealTransOutBean.setLoanAcct(loanAcct);
            dealTransOutBean.setLoanMain(loanMainInfo);
            dealTransOutBean.setPlanInfoList(dealPlanList);
            dealTransOutBean.setAcctList(dealAcctList);
            dealTransOutBean.setAccuAcctList(dealAccuAcctList);
            InvestorDelqTransOutSaveBean delqTransOutSaveBean = delqService.dealDelqTransOut(dealTransOutBean);
            if (null != delqTransOutSaveBean) {
                delqTransOutSaveBeanList.add(delqTransOutSaveBean);
            }
        }
        return delqTransOutSaveBeanList;
    }
}
